using System;
using Game.Board;
using HCore.Utility.Task;
using UI.UIGame;
using UI.UIGame.Menu.SortTile;
using UnityEngine;

namespace Session
{
    public class Session : IBoardSession, ISortTileSession, IUIGameSession
    {
        private Session(string jigsawImagePath, int hopeTileSize, int hopeTileCount, bool rotateTile)
        {
            Debug.Log($"Creating session for {jigsawImagePath}, hopeTileSize: {hopeTileSize}, hopeTileCount: {hopeTileCount}! ---------");
            JigsawImagePath = jigsawImagePath;
            HopeTileSize = hopeTileSize;
            HopeTileCount = hopeTileCount;
            RotateTile = rotateTile;
        }

        /// <summary>
        ///     希望拼图块大小
        /// </summary>
        public int HopeTileSize { get; }

        /// <summary>
        ///     希望拼图块数量
        /// </summary>
        public int HopeTileCount { get; }

        /// <summary>
        ///     拼图图片路径
        /// </summary>
        public string JigsawImagePath { get; }

        /// <summary>
        ///     初始化时，是否旋转 Tile
        /// </summary>
        public bool RotateTile { get; }

        /// <summary>
        ///     拼图板的信息
        /// </summary>
        public Board Board { get; private set; }

        /// <summary>
        ///     当前排序策略
        /// </summary>
        public SortPolicy CurrentSortPolicy { get; set; } = SortPolicy.None;

        /// <summary>
        ///     启动预排序 Tile
        /// </summary>
        public bool IsPreSortingStartedTile { get; set; } = false;

        /// <summary>
        ///     鼠标是否覆盖在游戏UI上
        /// </summary>
        public bool IsMouseOverlayOnGameUI { get; set; } = false;

        /// <summary>
        ///     初始化拼图板的信息
        /// </summary>
        public TaskQueue CreateBoard(Func<Texture2D> originalTexFunc)
        {
            Board = new Board();
            return new TaskQueue().AddTasks(Board!.SetupQueueTasks(originalTexFunc, HopeTileSize, HopeTileCount));
        }


        public override string ToString()
        {
            return $"HopeTileSize: {HopeTileSize}, " +
                   $"HopeTileCount: {HopeTileCount}, " +
                   $"JigsawImagePath: {JigsawImagePath}, " +
                   $"IsPreSortingStartedTile: {IsPreSortingStartedTile}, " +
                   $"IsMouseOverlayOnGameUI: {IsMouseOverlayOnGameUI}, " +
                   $"CurrentSortPolicy: {CurrentSortPolicy}";
        }

        /// <summary>
        ///     通过拼图图片路径和拼图块大小构建一个Session对象，用来贯穿正常拼图游戏，存放整场有的所有数据和行为。
        /// </summary>
        /// <param name="jigsawImagePath">拼图图片路径，建议不要太小，图片最短边，起码是拼图块的2倍及以上</param>
        /// <param name="hopeTileSize">希望的拼图块大小，不要设置太小，建议30以上</param>
        /// <param name="hopeTileCount">希望的拼图块的数量，这个优先级大于【hopeTileSize】。如果 小于或等于 0 则以【hopeTileSize】为准 </param>
        /// <param name="rotateTile">初始化时，是否旋转 Tile</param>
        /// <param name="debug">调试模式</param>
        /// <returns></returns>
        public static Session Create(string jigsawImagePath, int hopeTileSize, int hopeTileCount, bool rotateTile, bool debug = false)
        {
            var session = new Session(jigsawImagePath, hopeTileSize, hopeTileCount, rotateTile);
            if (debug)
            {
                // session.ResourceAu.Value = 500;
                // session.ResourceCu.Value = 500;
                // session.ResourceFe.Value = 500;
            }

            return session;
        }
    }
}